package com.black.app.service.impl.store;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.req.store.add.AppStoreCounponsAddReq;
import com.black.app.domain.req.store.edit.AppStoreCouponsEditReq;
import com.black.app.domain.req.store.query.AppStoreCouponsQueryReq;
import com.black.app.domain.resp.store.AppStoreCouponsResp;
import com.black.app.domain.store.AppStoreCoupons;
import com.black.app.service.store.AppStoreCouponsService;
import com.black.app.mapper.store.AppStoreCouponsMapper;
import com.black.common.exception.ServiceException;
import com.black.common.exception.database.PrimaryException;
import com.black.common.exception.database.RecordNotFoundException;
import com.black.common.utils.MessageUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
* @author 12572
* @description 针对表【app_store_coupons】的数据库操作Service实现
* @createDate 2024-10-25 16:02:51
*/
@Service
public class AppStoreCouponsServiceImpl extends ServiceImpl<AppStoreCouponsMapper, AppStoreCoupons>
    implements AppStoreCouponsService{

    @Override
    public PageInfo<AppStoreCouponsResp> paginationQuery(AppStoreCouponsQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    @Override
    public boolean add(AppStoreCounponsAddReq req) {
        //校验优惠券名称是否已存在
        if (this.baseMapper.exists(new LambdaQueryWrapper<AppStoreCoupons>().eq(AppStoreCoupons::getCouponName, req.getCouponName()))){
            throw new ServiceException(MessageUtils.message("appstorecoupons.couponName.exists"));
        }

        AppStoreCoupons newEntity = new AppStoreCoupons();

        BeanUtils.copyProperties(req, newEntity);

        boolean save = this.save(newEntity);

        return save;
    }

    @Override
    public AppStoreCouponsResp findById(Long id) {
        if (Objects.isNull(id)){
            throw new PrimaryException();
        }
        //根据主键查询
        AppStoreCoupons appStoreCoupons = this.getById(id);
        if (Objects.isNull(appStoreCoupons)){
            throw new RecordNotFoundException();
        }
        AppStoreCouponsResp resp = new AppStoreCouponsResp();
        BeanUtils.copyProperties(appStoreCoupons,resp);
        return resp;
    }

    @Override
    public boolean edit(AppStoreCouponsEditReq req) {
        //根据主键查询
        AppStoreCoupons appStoreCoupons = this.getById(req.getCouponId());
        if (Objects.isNull(appStoreCoupons)){
            throw new RecordNotFoundException();
        }

        //校验优惠券名称是否已存在
        if (this.baseMapper.exists(new LambdaQueryWrapper<AppStoreCoupons>().eq(AppStoreCoupons::getCouponName, req.getCouponName()).ne(AppStoreCoupons::getCouponId, req.getCouponId()))){
            throw new ServiceException(MessageUtils.message("appstorecoupons.couponName.exists"));
        }

        BeanUtils.copyProperties(req, appStoreCoupons);

        boolean res = this.updateById(appStoreCoupons);
        return res;
    }

    @Override
    public boolean deleteById(Long[] ids) {
        if (Objects.isNull(ids) || ids.length == 0) {
            throw new PrimaryException();
        }
        for (Long id : ids) {
            AppStoreCoupons appStoreCoupons = this.getById(id);
            if (Objects.nonNull(appStoreCoupons)) {
                this.removeById(id);
            }
        }
        return true;
    }
}




